﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.StockManage
{
    public partial class ScheduleView : AuthBasePage
    {
        public DataTable ScheduleDetailDT
        {
            set { Session["ScheduleDetailDT"] = value; }
            get
            {
                if (Session["ScheduleDetailDT"] == null)
                {
                    return CreateDt();
                }
                else
                {
                    return (DataTable)Session["ScheduleDetailDT"];
                }
            }
        }
        public DataTable CreateDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ScheduleDetailID");
            dt.Columns.Add("PlanID");
            dt.Columns.Add("WorkOrderNo");
            dt.Columns.Add("CustomerName");
            dt.Columns.Add("SendCustomerName");
            dt.Columns.Add("ProductID");
            dt.Columns.Add("ProductName");
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("OutSide");//外径
            dt.Columns.Add("InSide");//内径
            dt.Columns.Add("Unit");//单位
            dt.Columns.Add("OrderNums");//订单数量
            dt.Columns.Add("PlanNum");//计划数量=计划单数量+损耗余量
            dt.Columns.Add("LastNum");//剩余数量=计划单数量－累计排程数量
            dt.Columns.Add("ScheduleNum");//排程数量
            dt.Columns.Add("Remark");
            dt.Columns.Add("ExpectInStockDate");//预计入库日期
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }


        public DataTable ScheduleProcessDT
        {
            set { Session["ScheduleProcessDT"] = value; }
            get
            {
                if (Session["ScheduleProcessDT"] == null)
                {
                    return CreateProcessDt();
                }
                else
                {
                    return (DataTable)Session["ScheduleProcessDT"];
                }
            }
        }
        public DataTable CreateProcessDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ScheduleProcessID");
            dt.Columns.Add("ProcessID");
            dt.Columns.Add("ProcessName");
            dt.Columns.Add("OrderNum");
            dt.Columns.Add("IsCheck");
            dt.Columns.Add("IsOutSource");
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }



        public DataTable ScheduleMachineDT
        {
            set { Session["ScheduleMachineDT"] = value; }
            get
            {
                if (Session["ScheduleMachineDT"] == null)
                {
                    return CreatMachineDt();
                }
                else
                {
                    return (DataTable)Session["ScheduleMachineDT"];
                }
            }
        }
        public DataTable CreatMachineDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ScheduleMachineID");
            dt.Columns.Add("ScheduleProcessID");
            dt.Columns.Add("BillDate");
            dt.Columns.Add("ProcessID");
            dt.Columns.Add("MachineID");
            dt.Columns.Add("ExpectInputTime");
            dt.Columns.Add("SingleStandardProcessTime");
            dt.Columns.Add("AdjustTime");
            dt.Columns.Add("SingleClampTime");
            dt.Columns.Add("ExpectProduceTime");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ScheduleProcessDT = null;
                ScheduleDetailDT = null;
                ScheduleMachineDT = null;
                BindProcess();
                InitData();
            }
            txtScheduleID.Attributes.Add("readonly", "true");
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void InitData()
        {
            string ScheduleID = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            BindHead(ScheduleID);
        }
        /// <summary>
        /// 绑定工序和机台
        /// </summary>
        /// <param name="ScheduleID">排程明细ID</param>
        private void BindBodyProcess(string ScheduleDetailID)
        {
            //工序
            DataTable dt = BsScheduleBusniess.GetScheduleBodyProcessByID(ScheduleDetailID);
            DataTable dtemp = ScheduleProcessDT.Copy();
            //机台
            DataTable dtmachine = ScheduleMachineDT.Copy();
            foreach (DataRow dr in dt.Rows)
            {
                DataRow[] drs = dtemp.Select("ProcessID='" + CommonMethod.GetString(dr["ProcessID"]) + "'");
                if (drs.Length > 0)
                {
                    DataRow d = drs[0];
                    d["ScheduleProcessID"] = CommonMethod.GetString(dr["ScheduleProcessID"]);
                    d["OrderNum"] = CommonMethod.GetString(dr["OrderNum"]);
                    d["IsCheck"] = CommonMethod.GetString(dr["IsCheck"]);
                    d["IsOutSource"] = CommonMethod.GetString(dr["IsOutSource"]);
                    d["IsNew"] = "-1";
                    d["IsDel"] = "-1";
                    string ScheduleProcessID = CommonMethod.GetString(dr["ScheduleProcessID"]);
                    if (!string.IsNullOrEmpty(ScheduleProcessID.ToString()))
                    {
                        DataTable en = BsScheduleMachineBusiness.GetBsSchedulemachineByScheduleProcessID(ScheduleProcessID);
                        if (en != null && en.Rows.Count > 0)
                        {
                            DataRow macDr = en.Rows[0];
                            DataRow[] drr = dtmachine.Select("ScheduleProcessID='" + ScheduleProcessID + "' and ProcessID='" + CommonMethod.GetString(macDr["ProcessID"]) + "'");
                            if (drr.Length <= 0)
                            {
                                DataRow dro = dtmachine.NewRow();
                                dro["ScheduleMachineID"] = CommonMethod.GetString(macDr["ScheduleMachineID"]);
                                dro["ScheduleProcessID"] = CommonMethod.GetString(macDr["ScheduleProcessID"]);
                                dro["BillDate"] = CommonMethod.GetString(macDr["BillDate"]);
                                dro["ProcessID"] = CommonMethod.GetString(macDr["ProcessID"]);
                                dro["MachineID"] = CommonMethod.GetString(macDr["MachineID"]);
                                dro["ExpectInputTime"] = CommonMethod.GetString(macDr["ExpectInputTime"]);
                                dro["SingleStandardProcessTime"] = CommonMethod.GetString(macDr["SingleStandardProcessTime"]);
                                dro["AdjustTime"] = CommonMethod.GetString(macDr["AdjustTime"]);
                                dro["SingleClampTime"] = CommonMethod.GetString(macDr["SingleClampTime"]);
                                dro["ExpectProduceTime"] = string.Format("{0:yyyy-MM-dd}", macDr["ExpectProduceTime"]);
                                dtmachine.Rows.Add(dro);
                            }
                        }
                    }
                }
            }
            ScheduleMachineDT = dtmachine.Copy();//机台
            ScheduleProcessDT = dtemp.Copy();//工序
            grdProcess.DataSource = ScheduleProcessDT;
            grdProcess.DataBind();
        }

        /// <summary>
        /// 绑定产品
        /// </summary>
        /// <param name="ScheduleID">排程ID</param>
        private void BindHead(string ScheduleID)
        {
            //产品明细
            DataTable dt = BsScheduleBusniess.GetScheduleHeadByID(ScheduleID);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
                lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", dr["BillDate"]);
                lblFromDate.Text = string.Format("{0:yyyy-MM-dd}", dr["FromDate"]);
                lblEndDate.Text = string.Format("{0:yyyy-MM-dd}", dr["EndDate"]);

                DataTable dtemp = ScheduleDetailDT.Copy();
                foreach (DataRow drow in dt.Rows)
                {
                    DataRow d = dtemp.NewRow();
                    d["ScheduleDetailID"] = CommonMethod.GetString(drow["ScheduleDetailID"]);
                    d["PlanID"] = CommonMethod.GetString(drow["PlanID"]);
                    d["WorkOrderNo"] = CommonMethod.GetString(drow["WorkOrderNo"]);
                    d["CustomerName"] = CommonMethod.GetString(drow["CustomerName"]);
                    d["SendCustomerName"] = CommonMethod.GetString(drow["SendCustomerName"]);
                    d["ProductID"] = CommonMethod.GetString(drow["ProductID"]);
                    d["ProductName"] = CommonMethod.GetString(drow["ProductName"]);
                    d["Specifications"] = CommonMethod.GetString(drow["Specifications"]);
                    d["OutSide"] = CommonMethod.GetString(drow["OutSide"]);
                    d["InSide"] = CommonMethod.GetString(drow["InSide"]);
                    d["Unit"] = CommonMethod.GetString(drow["Unit"]);
                    d["OrderNums"] = CommonMethod.GetString(drow["OrderNums"]);
                    d["PlanNum"] = CommonMethod.GetString(drow["PlanNums"]);
                    d["LastNum"] = CommonMethod.GetString(drow["LastNum"]);
                    d["ScheduleNum"] = CommonMethod.GetString(drow["ScheduleNum"]);
                    d["Remark"] = CommonMethod.GetString(drow["Remark"]);
                    d["ExpectInStockDate"] = string.Format("{0:yyyy-MM-dd}", drow["ExpectInStockDate"]);
                    d["IsNew"] = "-1";
                    d["IsDel"] = "-1";
                    dtemp.Rows.Add(d);

                    BindBodyProcess(CommonMethod.GetString(drow["ScheduleDetailID"]));
                }
                ScheduleDetailDT = dtemp.Copy();
            }
            BindPro();
        }


        private void BindProcess()
        {
            DataTable dt = MdProcessBusiness.GetAllData();
            DataTable dtemp = ScheduleProcessDT.Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow drow = dtemp.NewRow();
                    drow["ScheduleProcessID"] = "-1";
                    drow["ProcessID"] = CommonMethod.GetString(dr["ProcessID"]);
                    drow["ProcessName"] = CommonMethod.GetString(dr["ProcessName"]);
                    drow["OrderNum"] = string.Empty;
                    drow["IsCheck"] = string.Empty;
                    drow["IsOutSource"] = string.Empty;
                    drow["IsNew"] = "1";
                    drow["IsDel"] = "-1";
                    dtemp.Rows.Add(drow);
                }
                ScheduleProcessDT = dtemp.Copy();
            }
            grdProcess.DataSource = ScheduleProcessDT;
            grdProcess.DataBind();
        }

        #region Product
        private void BindPro()
        {
            DataView dv = new DataView(ScheduleDetailDT);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            dt.Columns.Add("OrderNum");
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel4, "iframeAutoFit();");
        }
        #endregion


        protected void grdProcess_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkIsCheck = (CheckBox)e.Row.FindControl("chkIsCheck");
                CheckBox chkIsOutSource = (CheckBox)e.Row.FindControl("chkIsOutSource");
                string IsCheck = ((DataTable)((GridView)sender).DataSource).Rows[e.Row.RowIndex]["IsCheck"].ToString().Trim();
                string IsOutSource = ((DataTable)((GridView)sender).DataSource).Rows[e.Row.RowIndex]["IsOutSource"].ToString().Trim();
                if (!string.IsNullOrEmpty(IsCheck) && IsCheck.Equals("1"))
                {
                    chkIsCheck.Checked = true;
                }
                else
                {
                    chkIsCheck.Checked = false;
                }
                if (!string.IsNullOrEmpty(IsOutSource) && IsOutSource.Equals("1"))
                {
                    chkIsOutSource.Checked = true;
                }
                else
                {
                    chkIsOutSource.Checked = false;
                }
            }
        }
    }
}